﻿using System;
using System.Linq;

using Common.Enums;

namespace GameCommon.Base
{
    internal class ScreenChanger : IScreenChanger
    {
        private readonly Func<ScreenEnum, object, IScreen> addScreen;

        private readonly Action<ScreenEnum> removeScreen;
        
        public ScreenChanger(Func<ScreenEnum, object, IScreen> addScreen, Action<ScreenEnum> removeScreen)
        {
            this.addScreen = addScreen;
            this.removeScreen = removeScreen;
        }

        public IScreen AddScreen(ScreenEnum screen, object argument = null)
        {
            if (this.addScreen != null)
            {
                return this.addScreen(screen, argument);
            }

            return null;
        }

        public void RemoveScreen(ScreenEnum screen)
        {
            if (this.removeScreen != null)
            {
                this.removeScreen(screen);
            }
        }

        public void RemoveAnyScreen()
        {
            var screens = Enum.GetValues(typeof(ScreenEnum)).Cast<ScreenEnum>().ToList();

            foreach (var screen in screens)
            {
                if (this.removeScreen != null)
                {
                    this.removeScreen(screen);
                }
            }
        }
    }
}